package com.appkubes.mall.restful.application.promoter.aop;

import com.appkubes.mall.core.entity.MallPromoter;
import com.appkubes.mall.restful.application.promoter.entity.eo.UserRegisterEO;
import com.appkubes.mall.restful.application.promoter.service.PromoterService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Aspect
@Component
public class UserRegisterAop {

    @Resource
    PromoterService promoterService;

    @Pointcut("@annotation(com.appkubes.mall.restful.application.promoter.annotation.UserRegisterEvent)")
    public void UserRegister() {

    }

    @AfterReturning(returning = "userRegisterEO", pointcut = "UserRegister()")
    public void doUserRegisterAfter(JoinPoint joinPoint, UserRegisterEO userRegisterEO) {
        if (StringUtils.isEmpty(userRegisterEO.getPromoterKey())) {
            return;
        }

        log.info(String.format("推广员：%s,新增用户:%s", userRegisterEO.getPromoterKey(), userRegisterEO.getUserKey()));
        String promoterKey = userRegisterEO.getPromoterKey();
        MallPromoter mallPromoter = promoterService.getById(promoterKey);
        if (mallPromoter == null) {
            return;
        }
        //统计注册数
        mallPromoter.setStatisRegCount(mallPromoter.getStatisRegCount() + 1);
        promoterService.updateById(mallPromoter);
    }
}
